﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EffectsPack
{
    /// <summary>
    ///  Band pass filter with tunable 
    /// </summary>
    public class TunableBandPass:Effects.Filters.Filter
    {
        /// <summary>
        ///  Alfa coeffiients, controls filter's bandwidth. 
        /// </summary>
        private double alfa;

        /// <summary>
        ///  Gets or sets the alfa coefficient that controls the bandwidth. 2*alfa / (1+alfa^2) = cos (omega), where omega is normalized bandwidth.
        /// </summary>
        public double Alfa
        {
            get { return alfa; }
            set
            {
                lock (locker)
                {
                    alfa = value;
                }
            }
        }

        /// <summary>
        ///  Beta coefficient, controls center frequency. It is equal to cosinus of normalized center frequency.
        /// </summary>
        private double beta;

        /// <summary>
        ///  Gets or sets the Beta coefficients - cosine of normalized center frequency
        /// </summary>
        public double Beta
        {
            get { return beta; }
            set
            {
                lock (locker)
                {
                    beta = value;
                }
            }
        }

        /// <summary>
        ///  Locker for integrity reasons.
        /// </summary>
        object locker = new object();

        /// <summary>
        ///  Previous inputs and outputs.
        /// </summary>
        private double x1, x2, y1, y2;

        /// <summary>
        ///  Filters the signal
        /// </summary>
        /// <param name="x0"> Sample of input signal. </param>
        /// <returns> Filtered output signal. </returns>
        public override double Step(double x0)
        {
            double y0 = 0;
            lock (locker)
            {
                y0 = alfa * x0 - beta * (1 + alfa) * x1 + x2 + beta * (1 + alfa) * y1 - alfa * y2;
            }

            x2 = x1;
            x1 = x0;
            y2 = y1;
            y1 = y0;

            return y0;
        }
    }
}
